package com.zswl.bus;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2018/6/9 0009.
 */

public class ApiService {
    private static Api api;
    private static final int DEFAULT_TIMEOUT = 20;

    private ApiService() {

        //okhttp创建了
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addNetworkInterceptor(new CaheInterceptor(App.getAppInstance()))
                .addInterceptor(new LogInterceptor())
                .retryOnConnectionFailure(true)//失败重连
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        api = new Retrofit.Builder().baseUrl(Api.HOST)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(Api.class);


    }

    private static class Holder {
        private static ApiService INSTANCE = new ApiService();
    }

    public static ApiService getInstance() {
        return Holder.INSTANCE;
    }

    public Api getApi() {
        return api;
    }

}
